/**
  ******************************************************************************
  * @file    mt031x_can.h
  * @author  MIC Software Team
  * @version V1.0.0
  * @date    11/01/2015
  * @brief   This file contains all the functions prototypes for the CAN firmware 
  *          library.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, MIC SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2015 MIC</center></h2>
  ******************************************************************************  
  */ 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_H
#define __CAN_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "mt006.h"

/** @addtogroup MT031x_StdPeriph_Driver
  * @{
  */

/** @addtogroup CAN
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** 
  * @brief  CAN init structure definition
  */
typedef struct
{
	uint8_t CAN_BTR0;   		/*!< Specifies the Bus Timing Register0. */

	uint8_t CAN_BTR1;         /*!< Specifies the Bus Timing Register1. */

	uint8_t CAN_BTR2; 		/*!< Specifies the Bus Timing Register2. */
	                    
	uint8_t CAN_OCR;          /*!< Specifies the Output Control Register*/
} CAN_InitTypeDef;

/** 
  * @brief  CAN filter init structure definition
  */
typedef struct
{
	uint8_t CAN_FilterId0;             	/*!< Specifies the filter identification number */

	uint8_t CAN_FilterId1;              	/*!< Specifies the filter identification number */

	uint8_t CAN_FilterId2; 			  	/*!< Specifies the filter identification number */

	uint8_t CAN_FilterId3; 			   	/*!< Specifies the filter identification number */

	uint8_t CAN_FilterMaskId0;         	/*!< Specifies the filter mask number or identification number,
	                                          according to the mode .  */

	uint16_t CAN_FilterMaskId1; 		  	/*!< Specifies the filter mask number or identification number,
	                                          according to the mode . */

	uint8_t CAN_FilterMaskId2;          	/*!< Specifies the filter mask number or identification number,
	                                          according to the mode . */

	uint16_t CAN_FilterMaskId3; 		    /*!< Specifies the filter mask number or identification number,
	                                          according to the mode . */

	uint16_t CAN_FilterFIFOAssignment;    /*!< Specifies the FIFO (0 or 1) which will be assigned to the filter.
	                                          This parameter can be a value of @ref CAN_filter_FIFO */

	uint8_t CAN_FilterNumber;             /*!< Specifies the filter which will be initialized. It ranges from 0 to 13. */

	uint8_t CAN_FilterMode;               /*!< Specifies the filter mode to be initialized.
	                                          This parameter can be a value of @ref CAN_filter_mode */

	uint8_t CAN_FilterScale;              /*!< Specifies the filter scale.
	                                          This parameter can be a value of @ref CAN_filter_scale */

	FunctionalState CAN_FilterActivation; /*!< Enable or disable the filter.
	                                          This parameter can be set either to ENABLE or DISABLE. */
} CAN_FilterInitTypeDef;

/** 
  * @brief  CAN Tx message structure definition  
  */

typedef struct
{
	uint32_t StdId;  /*!< Specifies the standard identifier.
	                    This parameter can be a value between 0 to 0x7FF. */

	uint32_t ExtId;  /*!< Specifies the extended identifier.
	                    This parameter can be a value between 0 to 0x1FFFFFFF. */

	uint8_t FF;     /*!< Specifies the type of identifier for the message that will be transmitted.
	                    This parameter can be a value of @ref CAN_identifier_type */

	uint8_t RTR;     /*!< Specifies the type of frame for the message that will be transmitted.
	                    This parameter can be a value of @ref CAN_remote_transmission_request */

	uint8_t DLC;     /*!< Specifies the length of the frame that will be transmitted.
	                    This parameter can be a value between 0 to 8 */

	uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0 to 0xFF. */
} CanTxMsg;

/** 
  * @brief  CAN Rx message structure definition  
  */

typedef struct
{
	uint32_t StdId;  /*!< Specifies the standard identifier.
	                    This parameter can be a value between 0 to 0x7FF. */

	uint32_t ExtId;  /*!< Specifies the extended identifier.
	                    This parameter can be a value between 0 to 0x1FFFFFFF. */

	uint8_t FF;     /*!< Specifies the type of identifier for the message that will be received.
	                    This parameter can be a value of @ref CAN_identifier_type */

	uint8_t RTR;     /*!< Specifies the type of frame for the received message.
	                    This parameter can be a value of @ref CAN_remote_transmission_request */

	uint8_t DLC;     /*!< Specifies the length of the frame that will be received.
	                    This parameter can be a value between 0 to 8 */

	uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to 0xFF. */

	uint8_t FMI;     /*!< Specifies the index of the filter the message stored in the mailbox passes through.
	                    This parameter can be a value between 0 to 0xFF */
} CanRxMsg;

/* Exported constants --------------------------------------------------------*/

/** @defgroup CAN_Exported_Constants
  * @{
  */

/** @defgroup CAN_sleep_constants 
  * @{
  */
#define CANINITFAILED              ((uint8_t)0x00) /*!< CAN initialization failed */
#define CANINITOK                  ((uint8_t)0x01) /*!< CAN initialization failed */
/**
  * @}
  */

/** @defgroup CAN_Tx 
  * @{
  */
#define IS_CAN_STDID(STDID)   ((STDID) <= ((uint32_t)0x7FF))
#define IS_CAN_EXTID(EXTID)   ((EXTID) <= ((uint32_t)0x1FFFFFFF))
#define IS_CAN_DLC(DLC)       ((DLC) <= ((uint8_t)0x08))
/**
  * @}
  */

/** @defgroup CAN_InitTypeDef 
  * @{
  */
#define IS_CAN_BTR(BTR)   ((BTR) <= 0xFF)
#define IS_CAN_OCR(OCR)   ((OCR) <= 0xFF)
/**
  * @}
  */


/** @defgroup CAN_identifier_type 
  * @{
  */
#define CAN_ID_STD                 ((uint32_t)0x00000000)  /*!< Standard Id */
#define CAN_ID_EXT                 ((uint32_t)0x00000001)  /*!< Extended Id */
#define IS_CAN_IDTYPE(IDTYPE) (((IDTYPE) == CAN_ID_STD) || ((IDTYPE) == CAN_ID_EXT))
/**
  * @}
  */

/** @defgroup CAN_remote_transmission_request 
  * @{
  */
#define CAN_RTR_DATA                ((uint32_t)0x00000000)  /*!< Data frame */
#define CAN_RTR_REMOTE              ((uint32_t)0x00000001)  /*!< Remote frame */
#define IS_CAN_RTR(RTR) (((RTR) == CAN_RTR_DATA) || ((RTR) == CAN_RTR_REMOTE))
/**
  * @}
  */

/** @defgroup CAN_FilterID 
  * @{
  */
#define IS_CAN_ALL_FILTERID(FILTERID) ((FILTERID) <= 0xFF)
/**
  * @}
  */

/** @defgroup CAN_FilterMask 
  * @{
  */
#define IS_CAN_ALL_FILTERMASK(FILTERMASK) ((FILTERMASK) <= 0xFF)
/**
  * @}
  */

/** @defgroup CAN_filter_mode 
  * @{
  */
#define CAN_FilterMode_IdMask       ((uint8_t)0x00)  /*!< id/mask mode */
#define CAN_FilterMode_IdList       ((uint8_t)0x01)  /*!< identifier list mode */

#define IS_CAN_FILTER_MODE(MODE) (((MODE) == CAN_FilterMode_IdMask) || \
                                  ((MODE) == CAN_FilterMode_IdList))
/**
  * @}
  */

/** @defgroup CAN_interrupts 
  * @{
  */
#define CAN_IT_RIE                  ((uint8_t)0x01) /*!< Receive enable Interrupt*/
#define CAN_IT_TIE                  ((uint8_t)0x02) /*!< Transmit enable Interrupt*/
#define CAN_IT_EIE                  ((uint8_t)0x04) /*!< Error Warning Interrupt*/
#define CAN_IT_DOIE                 ((uint8_t)0x08) /*!< Data Overrupt Interrupt*/
#define CAN_IT_WUIE                 ((uint8_t)0x10) /*!< Wake-up Interrupt Interrupt*/
#define CAN_IT_EPIE                 ((uint8_t)0x20) /*!< Error Passive Interrupt*/
#define CAN_IT_ALIE                 ((uint8_t)0x40) /*!< Arbitration Lost Interrupt*/
#define CAN_IT_BEIE                 ((uint8_t)0x80) /*!< Bus Error Interrupt*/
/**
  * @}
  */

/**
  * @}
  */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/  
void CAN_DeInit(void);
uint8_t CAN_Init(CAN_InitTypeDef* CAN_InitStruct); 
void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct);
void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct);
void CAN_ITConfig(uint32_t CAN_IT, FunctionalState NewState);
uint8_t CAN_Transmit(CanTxMsg* TxMessage);
void CAN_Receive(CanRxMsg* RxMessage);
  

#ifdef __cplusplus
}
#endif

#endif /* __MT031X_CAN_H */
/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT MIC *****END OF FILE****/
